package cn.gemiman.daoyi.module.cms.controller.admin.appsite;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;
import cn.gemiman.daoyi.module.cms.controller.admin.appsite.vo.AppSitePageReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsite.vo.AppSiteRespVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsite.vo.AppSiteSaveReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitecomment.vo.AppSiteCommentUpdateStatusReqVO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsite.AppSiteDO;
import cn.gemiman.daoyi.module.cms.service.appsite.AppSiteService;
import cn.gemiman.daoyi.module.cms.service.tagdictionary.TagDictionaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 站点")
@RestController
@RequestMapping("/cms/app-site")
@Validated
public class AppSiteController {

    @Resource
    private AppSiteService appSiteService;
    @Resource
    private TagDictionaryService tagDictionaryService;

    @PostMapping("/create")
    @Operation(summary = "创建站点")
    @PreAuthorize("@ss.hasPermission('cms:app-site:create')")
    public CommonResult<Long> createAppSite(@Valid @RequestBody AppSiteSaveReqVO createReqVO) {
        return success(appSiteService.createAppSite(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新站点")
    @PreAuthorize("@ss.hasPermission('cms:app-site:update')")
    public CommonResult<Boolean> updateAppSite(@Valid @RequestBody AppSiteSaveReqVO updateReqVO) {
        appSiteService.updateAppSite(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-approve-status")
    @Operation(summary = "更新站点审核状态")
    @PreAuthorize("@ss.hasPermission('cms:app-site:update')")
    public CommonResult<Boolean> updateAppSiteCommentApproveStatus(@Valid @RequestBody AppSiteCommentUpdateStatusReqVO updateReqVO) {
        appSiteService.updateAppSiteApproveStatus(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新站点状态")
    @PreAuthorize("@ss.hasPermission('cms:app-site:update')")
    public CommonResult<Boolean> updateAppSiteCommentStatus(@Valid @RequestBody AppSiteCommentUpdateStatusReqVO updateReqVO) {
        appSiteService.updateAppSiteStatus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除站点")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:app-site:delete')")
    public CommonResult<Boolean> deleteAppSite(@RequestParam("id") Long id) {
        appSiteService.deleteAppSite(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得站点")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:app-site:query')")
    public CommonResult<AppSiteRespVO> getAppSite(@RequestParam("id") Long id) {
        AppSiteDO appSite = appSiteService.getAppSite(id);
        return success(BeanUtils.toBean(appSite, AppSiteRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得站点分页")
    @PreAuthorize("@ss.hasPermission('cms:app-site:query')")
    public CommonResult<PageResult<AppSiteRespVO>> getAppSitePage(@Valid AppSitePageReqVO pageReqVO) {
        PageResult<AppSiteDO> pageResult = appSiteService.getAppSitePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppSiteRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出站点 Excel")
    @PreAuthorize("@ss.hasPermission('cms:app-site:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppSiteExcel(@Valid AppSitePageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppSiteDO> list = appSiteService.getAppSitePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "站点.xls", "数据", AppSiteRespVO.class,
                BeanUtils.toBean(list, AppSiteRespVO.class));
    }

    @GetMapping("/getTagDictionary")
    @Operation(summary = "获取标签")
    public CommonResult<List<String>> getTagDictionary() {
        return success(tagDictionaryService.getTagDictionary());
    }
}
